<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	response.setHeader("X-Frame-Options","SAMEORIGIN");
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<jsp:include page="../../public/public.jsp"/>
<script type="text/javascript" src="../../them/jquery-easyui-1.4.2/ajaxfileupload.js"></script>
<script type="text/javascript">
	$(function(){
		init();
	})
	/* 条件查询 */
	function queryRefresh(){
			init();
	}
	/* 刷新列表 */
	function refresh(){
		$('#datagrid').datagrid("reload");
	}
	/*查询条件重置  */
	function chongzhi(){
		var linkName = $("#queryLinkNameId").textbox('setValue',''); 
		var linkType=$("#queryLinkTypeId").combobox('setValue','');
		var isDel=$("#queryIsDelId").combobox('setValue','');
	}
	function init(linkName,linkType,isDel) {
		var linkName = $("#queryLinkNameId").textbox('getValue').replace(/\s/g, ""); 
		var linkType=$("#queryLinkTypeId").combobox('getValue').replace(/\s/g, "");
		var isDel=$("#queryIsDelId").combobox('getValue').replace(/\s/g, "");
		$('#datagrid').datagrid({
			url : "<%=basePath%>link/queryLink", //获取数据地址
			iconCls : "icon-save", //图标
			pagination : true, //开启分页
			pageSize : 10, //每页显示数目 
			pageList : [ 10, 20, 30, 40, 50 ], //选择每页显示数目(与pageSize对应)
			fit : true, //适应大小
			fitColumns : true, //自动扩大或缩小列的尺寸以适应网格的宽度并且防止水平滚动
			nowrap : false, //是否关闭自动换行(true关闭，false开启)
			border : false, //边框
			remoteSort : false,
			singleSelect : true,
			rownumbers : true,
			queryParams: {
				imgTextType:'2',
				linkName:linkName,
				linkType:linkType,
				isDel:isDel
			},

			/* sortName : "createDate",
			sortOrder : 'desc', */
			onDblClickRow: function(index,row){//双击行事件
				//打开窗口
				update(row);
			},
			toolbar:menu_tb,//加载按钮功能
			columns : [ [ {
				title : "顺序",
				field : "orderId",
				align : 'center',
				width : 15
			},{
				title : "链接名称",
				field : "linkName",
				align : 'center',
				width : 80
			}, {
				title : "图片展示",
				field : "linkImg",
				align : 'center',
				width : 80,
				formatter : function(value ,row ,index){
					var linkImg =value;
			    	var imgstr = '<img  src="<%=basePath%>images/uploadimg/'+linkImg+'" style="width: 140px;height: 60px">';
			    	return imgstr;
				}
			}, {
				title : "链接地址",
				field : "linkUrl",
				align : 'center',
				width : 120,
				formatter : function(value, row, index) {
					return value;
				}
				
			},{
				title : "链接分类",
				field : "linkType",
				align : 'center',
				width : 60,
				formatter : function(value, row, index) {
				
					if(value==1){
						return "信用服务";
					}else if(value==2){
						return "联合惩戒系统";
					}else if(value==3){
						return "信用中国";
					}else if(value==4){
						return "信用山东";
					}else{
						return "未分类";
					}
				}
			},{
				title : "创建时间",
				field : "createDate",
				align : 'center',
				width : 50,
				formatter : function(value, row, index) {
					
					return formatDatebox(value);
				}
			},{
				title : "显示/隐藏",
				field : "isDel",
				align : 'center',
				width : 50,
				sortable : true,
				formatter : function(value, row, index) {
					
					if(value==1){
						
						return '<span><a href="#" style="text-decoration:none;color:green" onclick=del("'+row.linkId+'",'+value+',"'+row.linkName+'"'+'); >已 显 示</a></span>';
					}else if(value==2){
						
						return '<span><a href="#" style="text-decoration:none;color:red" onclick=del("'+row.linkId+'",'+value+',"'+row.linkName+'"'+'); >已 隐 藏</a></span>';
					}else{
						return "";
					}
				}
			}] ]
		});
	}
	//选中行触发
	function row_selected(){
		var row = $('#datagrid').datagrid('getSelected');
		if (row==null){
			$.messager.alert("友情提示","[请选择要修改的链接!]","error");
		}else{
			update(row);
		}
	}
	//打开添加用户窗口
	function showWindow(){
		
		//打开窗口
		$('#addForm').form('clear');
		$("#linkNameId").textbox('setValue','');
		//先对表单进行清空
		$("#menuimgId").val("");
	    $("#yuimgId").val("");
	    $("#yuimgId").css("display","none"); 
		$("#linkUrlId").textbox('setValue','');
		$("#orderId").textbox('setValue','');
		$('#linkTypeId').combobox('setValue','1')
		$('#add').show();
		$('#add').dialog({
			title: '新增链接',
			width: 400,
			height: 400,
			closed: false,
			cache: false,
			modal: true,
			buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					
				  	if($("#addForm").form('validate')){
				  		var istrue = yz();
						if(istrue == false){
							return;
						} 
						var linkName = $("#linkNameId").textbox('getValue').replace(/\s/g, "");
						var linkImg  = $("#menuimgId").val().replace(/\s/g, "");
						var orderId= $("#orderId").textbox('getValue').replace(/\s/g, "");
						var linkUrl = $("#linkUrlId").textbox('getValue').replace(/\s/g, "");
						var linkType=$("#linkTypeId").combobox('getValue').replace(/\s/g, "");
					  	$.ajax({  
							url:'<%=basePath %>link/addLink',
							type:'post',
							data:{
								'linkName':linkName,
								'linkUrl':linkUrl,
								'linkType':linkType,
								'orderId':parseInt(orderId),
								'imgTextType':'2',
								'linkImg':linkImg
							},
							dataType:'json',
							error:function(){
								$.messager.alert("友情提示","系统异常,请稍后尝试","error");
							}, 
							success:function(map){ 
								var r = map.row;
								if(r!=null && r==1){
									$.messager.alert("友情提示","[链接添加成功!]","info");
									$('#add').dialog('close');
									$('#datagrid').datagrid("reload");
								}else {
									$.messager.alert("友情提示","[sorry,链接添加失败!]","info");
								}
							}  
						});
				  	}
				    
				}
					
			},{
				text:'退出',
				iconCls:'icon-no',
				handler:function(){
					$('#add').dialog('close');
				}
			}]
		});
}

	function update(row){
		//赋值
		$("#linkNameId").textbox('setValue',row.linkName);
		var imgurl = row.linkImg
		$("#menuimgId").val(imgurl);
     	$("#yuimgId").attr("src","<%=basePath%>images/uploadimg/"+imgurl);
     	$("#yuimgId").val(imgurl);
    	$("#yuimgId").css("display","block");
    	    
    	$("#orderId").textbox('setValue',row.orderId);
		$("#linkUrlId").textbox('setValue',row.linkUrl);
		$('#linkTypeId').combobox('setValue',row.linkType);
		//打开窗口
		$('#add').show();
		$('#add').dialog({
			title: '修改链接',
			width: 400,
			height: 400,
			closed: false,
			cache: false,
			modal: true,
			buttons:[{
				text:'保存',
				iconCls:'icon-ok',
				handler:function(){
					var istrue = yz();
					if(istrue == false){
						return;
					}
					var linkName = $("#linkNameId").textbox('getValue').replace(/\s/g, ""); 
				    var linkImg = $("#menuimgId").val().replace(/\s/g, "");
					var linkUrl = $("#linkUrlId").textbox('getValue').replace(/\s/g, "");
					var orderId = $("#orderId").textbox('getValue').replace(/\s/g, "");
					var linkType=$("#linkTypeId").combobox('getValue').replace(/\s/g, "");
					  	$.ajax({  
							url:'<%=basePath %>link/updateLink',
							type:'post',
							data:{
								'linkId':row.linkId,
								'linkName':linkName,
								'linkUrl':linkUrl,
								'linkType':linkType,
								'orderId':parseInt(orderId),
								'linkImg':linkImg
							
							},
							dataType:'json',
							error:function(){
								$.messager.alert("友情提示","系统异常,请稍后尝试","error");
							}, 
							success:function(data){
								var r = data.row;
								if(r!=-1){
									$.messager.alert("友情提示","[链接修改成功!]","info");
									$('#add').dialog('close');
									$('#datagrid').datagrid("reload");
								}else {
									$.messager.alert("友情提示","[sorry,链接修改失败!]","info");
								}
							}  
						});
				  	}
			},{
				text:'退出',
				iconCls:'icon-no',
				handler:function(){
					$('#add').dialog('close');
				}
			}]
		});
	}
	//删除/显示/隐藏/视频
	function del(id,value,linkName){
		
		var linkId="";
		var linkStatus="";
		var row = $('#datagrid').datagrid('getSelected');
		var strStats = "";
		if(value==0){
			strStats="删除";
			if(row==null){
				$.messager.alert("友情提示","[请选择要"+strStats+"的链接]","info");
				return;
			}
			linkStatus=0
			linkName  = row.linkName;
			linkId = row.linkId;
			
		}
		if(value==1){
			linkStatus=2;
			linkId = id;
			strStats="隐藏";
		}
		if(value==2){
			linkStatus=1;
			linkId = id;
			strStats="显示";
		}
		if (row==null && linkStatus==0){
			$.messager.alert("友情提示","[请选择要"+strStats+"的链接]","info");
		}else{
			$.messager.confirm('系统提示', '[您确定要'+strStats+'链接:'+linkName+"吗?]", function(r) {
				if(r){
					$.ajax({  
						url:'<%=basePath %>link/updateLink',
						type:'post',
						data:{
							'linkId':linkId,
							'isDel':linkStatus
						},
						dataType:'json',
						error:function(){
							$.messager.alert("友情提示","正在维护,请稍后尝试","error");
						}, 
						success:function(data){ 
							var r = data.row;
							if(r != -1){
								$.messager.alert("友情提示","["+strStats+"成功!]","info");
								$('#datagrid').datagrid("reload");
							}else {
								$.messager.alert("友情提示","[sorry,"+strStats+"失败!]","info");
							}
						}  
					}); 
				}
			})
		}
	}
  function  yz(){
	    var linkName = $("#linkNameId").textbox('getValue').replace(/\s/g, ""); 
	    var linkImg = $("#menuimgId").val().replace(/\s/g, "");
		var linkUrl = $("#linkUrlId").textbox('getValue').replace(/\s/g, "");
		var linkType=$("#linkTypeId").combobox('getValue').replace(/\s/g, "");
		var orderId=$("#orderId").textbox('getValue').replace(/\s/g, "");
		if(linkName==""){
			$.messager.alert("友情提示","链接标题不允许为空","error");
			return false;
		}
		if(linkImg==""){
			$.messager.alert("友情提示","上传图片不允许为空","error");
			return false;
		}
		
		if(linkType==""|| linkType=="0"){
			$.messager.alert("友情提示","链接分类不允许为空","error");
			return false;
		}
		if(orderId==""||orderId=="0"){
			$.messager.alert("友情提示","加载顺序不允许为空且大于0","error");
			return false;
		}
		return true;
  }
  
</script>
</head>
<body class="easyui-layout">
	<div style="height: 100%; width: 100%">
		<!--添加窗口表单-->
		<div id="add" class="easyui-window" data-options="modal:true,closed:true,iconCls:'icon-edit'" style="padding: 10px;">
			<form id="addForm" method="post">
				<table align="center" style="margin-top: 10px">
					<tr style="height: 30px">
						<td>链接标题：</td>
						<td><input name="linkName" id="linkNameId" class="easyui-textbox" data-options="width:250,required:true" /></td>
					</tr>
					<tr style="height: 30px">
						<td>上传图片:</td>	
	  					<td>
	  						<input id="imgId" name="topImg" type="file" onchange="fn_img_upload()"/> 
		    				<input type="hidden" id="menuimgId"/>
	  					</td>
					</tr>
					
	  				<tr id="yulan" class="imageMsg">
	  					<td>图片预览:</td>
	  					<td>
	  						<img id="yuimgId" src="" style="display: none;width: 220px;height: 160px">
	  					</td>
	  				</tr>
					<tr style="height: 30px">
						<td>链接地址:</td>
						<td><input name="linkUrl" id="linkUrlId" class="easyui-textbox" data-options="width:250" /></td>
					</tr>
					<tr style="height: 30px">
						<td>加载顺序:</td>
						<td><input name="orderId" id="orderId" class="easyui-textbox" data-options="width:250,required:true" /></td>
					</tr>
					<tr style="height: 30px">
						<td>链接分类:</td>
						<td>
							<select name="linkType" id="linkTypeId" class="easyui-combobox" data-options="editable:false"> 
								<option value="1">信用服务</option>
								<option value="2">联合惩戒系统</option>
								<option value="3">信用中国</option>
								<option value="4">信用山东</option>
							</select>
						</td>
					</tr>
				</table>
			</form>
		</div>
		<div id="menu_tb">
		     <table>       
		     	<tr>
 		         	<td>
		                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-refresh" plain="true" onclick="refresh()">刷新列表</a>
		            </td>
		            <td>
		                <div class="datagrid-btn-separator"></div>
		            </td>
		            <td>
		                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-add" plain="true" onclick="showWindow()">新增链接</a>
		            </td>
		            <td>
		                <div class="datagrid-btn-separator"></div>
		            </td>
		            <td>
		            	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-edit" plain="true" onclick="row_selected()">修改链接</a>
		            </td>
		            <td>
		                <div class="datagrid-btn-separator"></div>
		            </td>
		            <td>
		            	<a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cut" plain="true" onclick="del(0,0,0)">删除链接</a>
		            </td>
		            
		            <td>
		                <div class="datagrid-btn-separator"></div>
		            </td>
		        </tr>
		       
		    </table>
		    <table>
		    	 <tr>
		        	<td>
		            	&nbsp;链接名称:<input id="queryLinkNameId" class="easyui-textbox" data-options="width:150" />
		            </td>
		            <td>
		            	&nbsp;
		                <div class="datagrid-btn-separator"></div>
		                &nbsp;
		            </td>
		            <td>
		            	链接分类:<select  id="queryLinkTypeId" class="easyui-combobox" data-options="editable:false"> 
								<option value="">全 部</option>
								<option value="1">信用服务</option>
								<option value="2">联合惩戒系统</option>
								<option value="3">信用中国</option>
								<option value="4">信用山东</option>
								
							</select>
		            </td>
		            
		            <td>
		            	&nbsp;
		                <div class="datagrid-btn-separator"></div>
		                &nbsp;
		            </td>
		              <td>
		            	显示/隐藏:<select  id="queryIsDelId" class="easyui-combobox" data-options="editable:false"> 
								<option value="">查 询 全 部&nbsp;</option>
								<option value="1"> 显 示 </option>
								<option value="2"> 隐 藏 </option>
							</select>
		            </td>
		            <td>
		                <div class="datagrid-btn-separator"></div>
		            </td>
		            <td>
		                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-refresh" plain="true" onclick="queryRefresh()">查询信息</a>
		            </td>
		             <td>
		                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-mini-refresh" plain="true" onclick="chongzhi()">条件重置</a>
		            </td>
		        </tr>
		    </table>
		</div>
	<table id="datagrid"></table>
</div>
<script type="text/javascript">

//图片上传
function fn_img_upload(){
	  var url = '<%=basePath%>upload/fileUpload';
	  var suffixStr = "jpg,JPG,png,PNG,gif,GIF,bmp,BMP";
	  var savePath = ",images,uploadimg";/* 图片存储路径 */
	  $.ajaxFileUpload({
          url: url+'?fileObjectId=topImg&MaxSize='+0+'&suffixStr='+suffixStr+'&savePath='+savePath+'&width='+0+'&height='+0, //用于文件上传的服务器端请求地址
          secureuri: false, //是否需要安全协议，一般设置为false
          fileElementId: 'imgId', //文件上传域的ID
          dataType: 'json', //返回值类型 一般设置为json
          success: function (data, status)  //服务器成功响应处理函数
          {
              if(data.success=="true"){
           	    $("#menuimgId").val(data.imgurl);
            	$("#yuimgId").attr("src","<%=basePath%>images/uploadimg/"+data.imgurl);
            	$("#yuimgId").val(data.imgurl);
           	    $("#yuimgId").css("display","block");
              }else{
           	   $.messager.alert("提示",data.msg,"error");
            		return;
              }
          },
          error: function (data, status, e)//服务器响应失败处理函数
          {
       	   $("#imgId").val("");
       	   $("#menuimgId").val("");
       	   $.messager.alert("提示",e,"error");
				return;
          }
   })
} 

/*对创建时间format  */	
function formatDatebox(value) {  
    if (value == null || value == '') {  
        return '';  
    }  
    var dt;  
    if (value instanceof Date) {  
        dt = value;  
    } else {  
        dt = new Date(value);  
    }  
  
    return dt.format("yyyy-MM-dd"); //扩展的Date的format方法(上述插件实现)  
}
	
	Date.prototype.format = function (format) {  
	    var o = {  
	        "M+": this.getMonth() + 1, // month  
	        "d+": this.getDate(), // day  
	        "h+": this.getHours(), // hour  
	        "m+": this.getMinutes(), // minute  
	        "s+": this.getSeconds(), // second  
	        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
	        "S": this.getMilliseconds()  
	        // millisecond  
	    }  
	    if (/(y+)/.test(format))  
	        format = format.replace(RegExp.$1, (this.getFullYear() + "")  
	            .substr(4 - RegExp.$1.length));  
	    for (var k in o)  
	        if (new RegExp("(" + k + ")").test(format))  
	            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
	    return format;  
} 
</script>
</body>
</html>